Thank you Datacamp!
https://www.datacamp.com/courses/interactive-data-visualization-with-bokeh
Loading all graphs may take a while. Please wait
Glyphs
from bokeh.plotting import figure, show
#from bokeh.io import output_file # output_file to save html of plot
from bokeh.io import output_notebook # plot file within this notebook
from bokeh.resources import INLINE # without this plots won't appear when opened as HTML
x = [1.7, 2.6, 2.0, 2.5, 3.1]
y = [10, 13, 29, 40, 32]
# Create the figure: p - figure = the graph itself
p1 = figure(x_axis_label ='some x', y_axis_label='some y')
# Circle Glyph
# glpyh is data marker - so marker here is a circle
# Add a circle glyph to the figure p
p1.circle(x, y, color = 'blue', size = 10, alpha = 0.8)
# x Glyph
# some more data for a new marker x
x_1 = [10,20,40,30,10]
y_1 = [12,65,34,23,67]
p1.x(x_1, y_1, color = 'red', size = 10, alpha = 0.8) # marker = x
output_notebook(resources=INLINE)
show(p1) # Display the plot
# Line Glyph and circle marker
# with date on x axis
import datetime
t = [datetime.datetime(2017,2,20,0,0), datetime.datetime(2017,3,21,0,0), datetime.datetime(2017,4,22,0,0),
datetime.datetime(2017,5,23,0,0), datetime.datetime(2017,6,24,0,0)]
p2 = figure(x_axis_type= 'datetime', x_axis_label='Date', y_axis_label='US Dollars')
p2.line(t, y) # line glyph
p2.circle(t, y, fill_color='white', size=6) # circle marker
output_notebook(resources=INLINE)
show(p2)
Patches
# Patches
# needs a list of lists. inner list is the coordinates of each patch, outer one is coordinates for placement of patches
import numpy as np
tri_x = [1,2,3,1]
tri_y = [1,2,1,1]
square_x = [2,3,4,3,2]
square_y = [2,3,2,1,2]
tri_x_p = np.array(tri_x) * 5
tri_y_p = np.array(tri_y) * 5
square_x_p = np.array(square_x) * 5
square_y_p = np.array(square_y) * 5
x = [tri_x_p, square_x_p]
y = [tri_y_p, square_y_p]
p = figure(x_axis_label ='some x', y_axis_label='some y')
p.line(tri_x, tri_y)
p.line(square_x, square_y, color = 'red')
p.patches(x, y, line_color = 'white') # patches of the same figure
output_notebook(resources=INLINE)
show(p)
# similarly can use lists, np array, dataframes and ColumnDataSource(next up) as data sources
ColumnDataSource
The ColumnDataSource is a table-like data object that maps string column names to sequences (columns) of data.
It is the central and most common data structure in Bokeh.
df -> ColumnDataSource is easy, not the other way round
from bokeh.plotting import ColumnDataSource
import pandas as pd
url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/titanic.csv'
titanic = pd.read_csv(url)
source = ColumnDataSource(titanic)
p = figure(x_axis_label ='some x', y_axis_label='some y')
p.circle('age', 'fare', size = 8, source = source)
output_notebook(resources=INLINE)
show(p)
HoverTool
from bokeh.models import HoverTool
p = figure(x_axis_label ='some x', y_axis_label='some y')
p.circle('age', 'fare', size = 8, source = source,
fill_color='grey', alpha=0.1, line_color=None,
hover_fill_color='firebrick', hover_alpha=0.5,
hover_line_color='white')
hover = HoverTool(mode = 'vline', tooltips = None) # draws a vertical line when hovered
p.add_tools(hover)
output_notebook(resources=INLINE)
show(p)
Color based on a data column value
from bokeh.models import CategoricalColorMapper
color_mapper = CategoricalColorMapper(factors=['First', 'Second', 'Third'],
palette=['red', 'green', 'blue'])
p = figure(x_axis_label ='some x', y_axis_label='some y')
p.circle('age', 'fare', source=source,
color=dict(field='class', transform=color_mapper),
legend='class')
output_notebook(resources=INLINE)
show(p)
Plot Layouts
### rows
from bokeh.layouts import row
layout = row(p1,p2)
output_notebook(resources=INLINE)
show(layout)
### columns
from bokeh.layouts import column
# layout = column(p1,p2)
# Combination of rows and columns
# layout = row(p1, [p2,p3])
### Grids
from bokeh.layouts import gridplot
# row1 = [p1,p2]
# row2 = [p3,p4]
# layout = gridplot([row1,row2])
### Panels or Tabs
from bokeh.models.widgets import Panel
# tab1 = Panel(child=p1, title='Latin America')
# tab2 = Panel(child=p2, title='Africa')
# tab3 = Panel(child=p3, title='Asia')
# tab4 = Panel(child=p4, title='Europe')
from bokeh.models.widgets import Tabs
# layout = Tabs(tabs=[tab1, tab2, tab3, tab4])
Linking Plots